<?php
print "PDO";
$connectionString  = "mysql:dbname=blog;";
$connectionString .= "host=127.0.0.1;";
$db_login = "root";
$db_pass = "lapute";
try
{
	$conn = new PDO($connectionString, $db_login, $db_pass);
}
catch( Exception $e )
{
	echo "Error occured : ".$e->getMessage();
}
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
$xml .= "<?xml-stylesheet href=\"style.xsl\" type=\"text/xsl\"?>\n";
$xml .= "<BLOG>\n";

$sql = "select * from comments";
$select = $conn->query($sql);

$xml .= "\t<comment>\n";
while ($result = $select->fetch(PDO::FETCH_ASSOC)){
	$xml .= "\t\t<comm>\n";
	$xml .= "\t\t\t<id>".$result["id"]."</id>\n";
	$xml .= "\t\t\t<body>".format_result($result["body"])."</body>\n";
	$xml .= "\t\t\t<date_time>".$result["date_time"]."</date_time>\n";
	$xml .= "\t\t\t<id_user>".$result["id_user"]."</id_user>\n";
	$xml .= "\t\t\t<id_news>".$result["id_news"]."</id_news>\n";
	$xml .= "\t\t</comm>\n";
}
$xml .= "\t</comment>\n";

$sql = "select * from user";
$select = $conn->prepare($sql);
$select->execute();
$xml .= "\t<user>\n";
while ($result = $select->fetch(PDO::FETCH_ASSOC)){
	$xml .= "\t\t<us>\n";
	$xml .= "\t\t\t<id>".$result["id"]."</id>\n";
	$xml .= "\t\t\t<username>".$result["username"]."</username>\n";
	$xml .= "\t\t\t<password>".$result["password"]."</password>\n";
	$xml .= "\t\t\t<age>".$result["age"]."</age>\n";
	$xml .= "\t\t</us>\n";
}
$xml .= "\t</user>\n";

$sql = "select * from news";
$select = $conn->prepare($sql);
$select->execute();
$xml .= "\t<news>\n";
while ($result = $select->fetch(PDO::FETCH_ASSOC)){
	$xml .= "\t\t<nw>\n";
	$xml .= "\t\t\t<id>".$result["id"]."</id>\n";
	$xml .= "\t\t\t<title>".format_result($result["title"])."</title>\n";
	$xml .= "\t\t\t<body>".format_result($result["body"])."</body>\n";
	$xml .= "\t\t\t<date_time>".$result["date_time"]."</date_time>\n";
	$xml .= "\t\t\t<id_user>".$result["id_user"]."</id_user>\n";
	$xml .= "\t\t</nw>\n";
}
$xml .= "\t</news>\n";

$xml .= "</BLOG>\n";

$file = fopen("resultat.xml", "w+");
fwrite($file,$xml);
fclose($file);

function format_result($str){
	$str = str_replace("\n","\\n",$str);
	$str = str_replace("\r","\\r",$str);
	$str = str_replace("\t","\\t",$str);
	return $str;
}
?>
